/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iapractica.views;

import genetics.individuos.Individuo;
import genetics.individuos.IndividuoSobrComparator;
import java.util.Collections;

import java.util.LinkedList;


/**
 *
 * @author Justo Vargas
 */
public final class PopulationView extends javax.swing.JFrame {
    
      
    /**
     * Creates new form ResultsView
     */
    public PopulationView() {
        initComponents();
        
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(iapractica.IAPracticaApp.class).getContext().getResourceMap(PopulationView.class);
        this.setIconImage(resourceMap.getImageIcon("image.icon").getImage());
        
         Object[][] objectToModel = new Object[1][8];

        for (int i = 0; i < 1; i++) {
            Object[] filas = objectToModel[i];
            filas[0] = i + 1;
            filas[1] = 0;
            filas[2] = 0;
            filas[3] = 0;
            filas[4] = 0;
            filas[5] = 0;
            filas[6] = 0;
            filas[7] = 0;
        }

        populationActual.setModel(new javax.swing.table.DefaultTableModel(
                objectToModel,
                new String[]{
                    "Solucion Nro", "Valor Aptitud", "Ganancia","Materia Prima Sobrante", "Productos 1", "Productos 2", "Productos 3", "Productos 4"
                }));
        
        BeforePopulation.setModel(new javax.swing.table.DefaultTableModel(
                objectToModel,
                new String[]{
                    "Solucion Nro", "Valor Aptitud", "Ganancia","Materia Prima Sobrante", "Productos 1", "Productos 2", "Productos 3", "Productos 4"
                }));
    }
    
    public void setTableContents(LinkedList<Individuo> myFinalPopulation, int age){
        
        this.setTitle("GA-Solver - Poblacion Actual - Iteracion Numero "+age);
        
        BeforePopulation.setModel(populationActual.getModel());

        Object[][] objectToModel = new Object[myFinalPopulation.size()][8];

        for (int i = 0; i < myFinalPopulation.size(); i++) {
            Object[] filas = objectToModel[i];
            filas[0] = i + 1;
            Individuo cromosoma = myFinalPopulation.get(i);
            filas[1] = cromosoma.getFitnessValue();
            filas[2] = cromosoma.getProfit();
            filas[3] = cromosoma.getMateriaPrimaSobrante();
            filas[4] = cromosoma.getProductsSize(1);
            filas[5] = cromosoma.getProductsSize(2);
            filas[6] = cromosoma.getProductsSize(3);
            filas[7] = cromosoma.getProductsSize(4);
        }

        populationActual.setModel(new javax.swing.table.DefaultTableModel(
                objectToModel,
                new String[]{
                    "Solucion Nro", "Valor Aptitud", "Ganancia","Materia Prima Sobrante", "Productos 1", "Productos 2", "Productos 3", "Productos 4"
                }));
        
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        actualPopulationView = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        populationActual = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };
        beforePopulationPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        BeforePopulation = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(iapractica.IAPracticaApp.class).getContext().getResourceMap(PopulationView.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setAutoscrolls(true);
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout(2, 0));

        actualPopulationView.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("actualPopulationView.border.title"))); // NOI18N
        actualPopulationView.setName("actualPopulationView"); // NOI18N
        actualPopulationView.setLayout(new java.awt.GridLayout(1, 0));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        populationActual.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"sr", "fv", null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Valor Aptitud", "Profit", "m1", "Title 4", "Title 5", "Title 6", "Title 7", "Title 8", "Title 9", "Title 10"
            }
        ));
        populationActual.setName("populationActual"); // NOI18N
        populationActual.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                populationActualMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(populationActual);

        actualPopulationView.add(jScrollPane1);

        jPanel2.add(actualPopulationView);

        beforePopulationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("beforePopulationPanel.border.title"))); // NOI18N
        beforePopulationPanel.setName("beforePopulationPanel"); // NOI18N
        beforePopulationPanel.setLayout(new java.awt.GridLayout(1, 0));

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        BeforePopulation.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"sr", "fv", null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Valor Aptitud", "Profit", "m1", "Title 4", "Title 5", "Title 6", "Title 7", "Title 8", "Title 9", "Title 10"
            }
        ));
        BeforePopulation.setName("BeforePopulation"); // NOI18N
        BeforePopulation.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BeforePopulationMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(BeforePopulation);

        beforePopulationPanel.add(jScrollPane2);

        jPanel2.add(beforePopulationPanel);

        jPanel1.add(jPanel2);

        getContentPane().add(jPanel1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void populationActualMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_populationActualMouseClicked
       
    }//GEN-LAST:event_populationActualMouseClicked

    private void BeforePopulationMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BeforePopulationMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_BeforePopulationMouseClicked

    

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable BeforePopulation;
    private javax.swing.JPanel actualPopulationView;
    private javax.swing.JPanel beforePopulationPanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable populationActual;
    // End of variables declaration//GEN-END:variables

    private Individuo getIndBySobrante(LinkedList<Individuo> myFinalPopulation, Integer sobrante) {
        for (Individuo individuo : myFinalPopulation) {
            if (individuo.getMateriaPrimaSobrante() == sobrante){
                return individuo;
            }
        }
        return null;
    }
}
